package com.rumor.background.Controller;

import com.github.pagehelper.PageHelper;
import com.rumor.background.Mapper.AdminMapper;
import com.rumor.background.Utils.Admin.AdminUtils;
import com.rumor.background.Utils.Utils;
import com.rumor.background.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@RestController
public class AdminController {

    @Autowired
    AdminMapper ram;

//    @GetMapping("/getImgLink")
//    public String[] getRumorImgLink(HttpServletRequest request) throws IOException {
////        String[] imgList = Utils.getImgList("2020-11-15/39d6o8Q1/");
////        String link = request.getScheme() + "://" + request.getServerName() + ":"  + request.getServerPort() + "/";
////        for(int i = 0; i < imgList.length; i++){
////            imgList[i] = link + imgList[i].replace("\\", "/");
////        }
////        for(String i : imgList){
////            System.out.println(i);
////        }
////        return imgList;
//    }

    @PostMapping("/adminLogin")
    public String adminLogin(HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Administrator adm = ram.queryAdminByUsername(username);
        if(!password.equals(adm.getPassword())) return "-1";
        String admin_code = Utils.getAdminCode();
        while(admin_code.equals(ram.queryAdminCodeByAdminCode(admin_code))) admin_code = Utils.getAdminCode();
        if(ram.updateAdminCode(username, admin_code) > 0)
            return admin_code;
        return "-1";
    }

    /*
     * rumor投稿相关接口
     */
    @PostMapping("/getRumorContribute")
    public Object getRumorContribute(HttpServletRequest request) throws IOException {
        if(AdminUtils.checkLoginType(request, ram)) return null;

        PageHelper.startPage(Integer.parseInt(request.getParameter("pageNum")), 20);
        List<RumorContributeBackground> rcb = ram.queryAllRumorContribute();
        return AdminUtils.getPageResultForRumor(request, rcb);
    }

    @PostMapping("/getRumorContributeByDate")
    public Object getRumorContributeByDate(HttpServletRequest request) throws IOException {
        if(AdminUtils.checkLoginType(request, ram)) return null;
        String date = request.getParameter("date");

        PageHelper.startPage(Integer.parseInt(request.getParameter("pageNum")), 20);
        List<RumorContributeBackground> rcb = ram.queryRumorContributeByDate(date);
        return AdminUtils.getPageResultForRumor(request, rcb);
    }

    @PostMapping("/getRumorContributeByMark")
    public Object getMarkedRumorContribute(HttpServletRequest request) throws IOException {
        if(AdminUtils.checkLoginType(request, ram)) return null;
        String mark = request.getParameter("mark");

        PageHelper.startPage(Integer.parseInt(request.getParameter("pageNum")), 20);
        List<RumorContributeBackground> rcb = ram.queryAllMarkedRumorContribute(mark);
        return AdminUtils.getPageResultForRumor(request, rcb);
    }

    @PostMapping("/markRumorContribute")
    public int markRumorContribute(HttpServletRequest request){
        if(AdminUtils.checkLoginType(request, ram)) return -1;
        return ram.markRumorContribute(request.getParameter("rumor_code"), request.getParameter("mark"));
    }

    /*
     * 失物招领相关接口
     */
    @PostMapping("/getFound")
    public Object getFound(HttpServletRequest request) throws IOException {
        if(AdminUtils.checkLoginType(request, ram)) return null;

        PageHelper.startPage(Integer.parseInt(request.getParameter("pageNum")), 20);
        List<FoundBackground> fd = ram.queryAllFound();
        return AdminUtils.getPageResultForFound(request, fd);
    }

    @PostMapping("/getFoundByDate")
    public Object getFoundByDate(HttpServletRequest request) throws IOException {
        if(AdminUtils.checkLoginType(request, ram)) return null;
        String date = request.getParameter("date");

        PageHelper.startPage(Integer.parseInt(request.getParameter("pageNum")), 20);
        List<FoundBackground> fd = ram.queryFoundByDate(date);
        return AdminUtils.getPageResultForFound(request, fd);
    }

    @PostMapping("/getFoundByMark")
    public Object getFoundByMark(HttpServletRequest request) throws IOException {
        if(AdminUtils.checkLoginType(request, ram)) return null;
        String mark = request.getParameter("mark");

        PageHelper.startPage(Integer.parseInt(request.getParameter("pageNum")), 20);
        List<FoundBackground> fd = ram.queryAllMarkedFound(mark);
        return AdminUtils.getPageResultForFound(request, fd);
    }

    @PostMapping("/markFound")
    public int markFound(HttpServletRequest request){
        if(AdminUtils.checkLoginType(request, ram)) return -1;
        return ram.markFound(request.getParameter("found_code"), request.getParameter("mark"));
    }

    /*
     * 寻物启事相关接口
     */
    @PostMapping("/getLost")
    public Object getLost(HttpServletRequest request) throws IOException {
        if(AdminUtils.checkLoginType(request, ram)) return null;

        PageHelper.startPage(Integer.parseInt(request.getParameter("pageNum")), 20);
        List<LostBackground> ls = ram.queryAllLost();
        return AdminUtils.getPageResultForLost(request, ls);
    }

    @PostMapping("/getLostByDate")
    public Object getLostByDate(HttpServletRequest request) throws IOException {
        if(AdminUtils.checkLoginType(request, ram)) return null;
        String date = request.getParameter("date");

        PageHelper.startPage(Integer.parseInt(request.getParameter("pageNum")), 20);
        List<LostBackground> ls = ram.queryLostByDate(date);
        return AdminUtils.getPageResultForLost(request, ls);
    }

    @PostMapping("/getLostByMark")
    public Object getLostByMark(HttpServletRequest request) throws IOException {
        if(AdminUtils.checkLoginType(request, ram)) return null;
        String mark = request.getParameter("mark");

        PageHelper.startPage(Integer.parseInt(request.getParameter("pageNum")), 20);
        List<LostBackground> ls = ram.queryAllMarkedLost(mark);
        return AdminUtils.getPageResultForLost(request, ls);
    }

    @PostMapping("/markLost")
    public int markLost(HttpServletRequest request){
        if(AdminUtils.checkLoginType(request, ram)) return -1;
        return ram.markLost(request.getParameter("lost_code"), request.getParameter("mark"));
    }
}
